import os
import sys
import yaml
import glob

# Check Python version and encoding settings
print(f"Python version: {sys.version}")
print(f"Default encoding: {sys.getdefaultencoding()}")
print(f"FileSystem encoding: {sys.getfilesystemencoding()}")

# Try to find Conda configuration files
conda_config_paths = [
    os.path.expanduser('~/.condarc'),
    os.path.join(os.environ.get('CONDA_PREFIX', ''), '.condarc'),
    'D:\\ProgramData\\anaconda3\\.condarc',
]

print("\nChecking Conda configuration files:")
for config_path in conda_config_paths:
    if os.path.exists(config_path):
        print(f"Found config file: {config_path}")
        try:
            # Try to read with UTF-8 encoding
            with open(config_path, 'r', encoding='utf-8') as f:
                content = f.read()
                print(f"  - Successfully read with UTF-8")
                print(f"  - File size: {len(content)} bytes")
                # Try to parse as YAML
                try:
                    parsed = yaml.safe_load(content)
                    print(f"  - Successfully parsed as YAML")
                except Exception as e:
                    print(f"  - Error parsing YAML: {e}")
        except UnicodeDecodeError:
            print(f"  - Failed to read with UTF-8")
            try:
                # Try with GBK encoding
                with open(config_path, 'r', encoding='gbk') as f:
                    content = f.read()
                    print(f"  - Successfully read with GBK")
                    # Show problematic section
                    if len(content) > 100:
                        print(f"  - Sample content (first 100 chars): {content[:100]}")
            except UnicodeDecodeError:
                print(f"  - Failed to read with GBK")
    else:
        print(f"Config file not found: {config_path}")

print("\nSuggestions to fix Conda encoding issues:")
print("1. Set environment variable: set CONDA_ENCODING=utf-8")
print("2. Try using --no-plugins flag: conda --no-plugins info")
print("3. Create/modify .condarc file with UTF-8 encoding")
print("4. Use pip directly instead of conda for package installation")